<template>
  <div class="list">
    <!-- 添加按钮 -->
    <el-button type="primary" class="btn-class" @click="openAddDialogVisible()">添加</el-button>

    <!-- 表格 -->
    <el-table :data="listType" border style="width: 100%">
      <el-table-column prop="typeId" label="编号" />
      <el-table-column prop="typeName" label="类型" />
      <el-table-column prop="typeBlogCount" label="博客数" />
      <el-table-column prop="enable" label="是否启用">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.enable == 1">启用</el-tag>
          <el-tag v-else type="danger">未启用</el-tag>
        </template>
      </el-table-column>

      <!-- 操作 -->

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button type="primary">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handleEdit(scope.row.typeId)">编辑</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-if="scope.row.enable ===0 " size="mini" type="success" @click="toEnable(scope.row.typeId)">启用</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-if="scope.row.enable ===1 " size="mini" type="warning" @click="toDisable(scope.row.typeId)">弃用</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="danger" @click="handleDelete(scope.row.typeId)">删除</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加 -->
    <el-dialog title="添加" :visible.sync="dialogVisible">
      <type-add @closeAddDialogVisible="closeAddDialogVisible" @getTypeList="getTypeList" />
    </el-dialog>

    <!-- 修改 -->
    <el-dialog title="修改" :visible.sync="updateDialog">
      <type-update :type="type" @closeUpdateDialog="closeUpdateDialog" @getTypeList="getTypeList" />
    </el-dialog>
  </div>
</template>

<script>
// 导入typeApi
import typeApi from '@/api/type'
import TypeAdd from './type-add'
import TypeUpdate from './type-update'
export default {
  name: 'TypeList',
  components: {
    TypeAdd,
    TypeUpdate
  },
  data() {
    return {
      updateDialog: false,
      dialogVisible: false,
      listType: [],
      type: {}
    }
  },
  // 钩子函数，实例化之前执行
  created() {
    this.getTypeList()
  },
  methods: {
    getTypeList() {
      typeApi.listBack().then(res => {
        console.log(res.data)
        // 赋值到集合列表中
        this.listType = res.data
      })
    },
    handleEdit(id) {
      // 先查询
      typeApi.get(id).then(res => {
        this.type = res.data
        // 修改 开启弹框
        this.updateDialog = true
      })
    },
    handleDelete(id) {
      // 删除
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用删除接口
        typeApi.deleteById(id).then(res => {
          this.$message.success(res.msg)
          // 刷新表格
          this.getTypeList()
        })
      })
    },
    // 添加
    openAddDialogVisible() {
      this.dialogVisible = true
    },
    // 关闭添加弹窗
    closeAddDialogVisible() {
      this.dialogVisible = false
    },
    // 关闭修改弹窗
    closeUpdateDialog() {
      this.updateDialog = false
    },
    // 启用
    toEnable(id) {
      this.$confirm('是否启用该分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        typeApi.getEnable(id).then(res => {
          this.$message.success(res.msg)
          // 刷新表格
          this.getTypeList()
        })
      })
    },
    // 弃用
    toDisable(id) {
      this.$confirm('是否弃用该分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        typeApi.getDisable(id).then(res => {
          this.$message.success(res.msg)
          // 刷新表格
          this.getTypeList()
        })
      })
    }
  }
}
</script>

